package com.itheima.servlet01;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

//响应图片到浏览器

@WebServlet("/ServletDemo333")
public class ServletDemo333 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //通过文件的相对路劲得到文件的绝对路径
        String realPath = getServletContext().getRealPath("/img/hm.png");
        System.out.println(realPath);

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(realPath));

        ServletOutputStream sos = resp.getOutputStream();
        byte[] bytes = new byte[1025];
        int len;
        while ((len=bis.read(bytes))!=-1){
            sos.write(bytes,0,len);
        }
        bis.close();
        sos.close();

    }






    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
